package cn.xiaoyu.snake;

import com.joanzapata.iconify.IconFontDescriptor;
import com.joanzapata.iconify.Iconify;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by fengzhe on 2017/7/17.
 */

public class Configuration {

    private static final HashMap<String, Object> SNAKE_CONFIGS = new HashMap<>();

    private static final ArrayList<IconFontDescriptor> ICONS = new ArrayList<>();

    private Configuration() {
        SNAKE_CONFIGS.put(ConfigType.CONFIG_READY.name(), false);
    }

    private static class Holder {
        private static final Configuration ISNSTANCE = new Configuration();
    }

    public final void configure() {
        initIcons();
        SNAKE_CONFIGS.put(ConfigType.CONFIG_READY.name(), true);
    }

    public static Configuration getInstance() {
        return Holder.ISNSTANCE;
    }

    final HashMap<String, Object> getConfigurations() {
        return SNAKE_CONFIGS;
    }

    public final Configuration withApiHost(String host) {
        SNAKE_CONFIGS.put(ConfigType.API_HOST.name(), host);
        return this;
    }

    private void initIcons() {
        if (ICONS.size() > 0) {
            final Iconify.IconifyInitializer initializer = Iconify.with(ICONS.get(0));
            for (int i = 1; i < ICONS.size(); i++) {
                initializer.with(ICONS.get(i));
            }
        }
    }

    public final Configuration withIcons(IconFontDescriptor icon) {
        ICONS.add(icon);
        return this;
    }

    private void checkConfiguration() {
        final boolean isReady = (boolean) SNAKE_CONFIGS.get(ConfigType.CONFIG_READY);
        if (!isReady) {
            throw new RuntimeException("Configuration is not ready,call configure()");
        }
    }

    final <T> T getConfiguration(Enum<ConfigType> key) {
        checkConfiguration();
        return (T) SNAKE_CONFIGS.get(key.name());
    }
}


